# import json
import os
import sys
from urllib.parse import quote
import yaml

# from comm.dateTime import nowDay
# from comm.privateKey import privateKeyData
from comm.secretKey import jieMi


# def openDbBase():
#     if getattr(sys, 'frozen', False):
#         current_file_path = sys.executable
#         parent_directory = os.path.dirname(current_file_path)
#     else:
#         current_file_path = os.path.abspath(__file__)
#         project_path = os.path.dirname(current_file_path)
#
#         # 获取上层路径
#         parent_directory = os.path.dirname(project_path)
#     file_path = os.path.join(parent_directory + '/', 'config.yaml')
#     if os.path.isfile(file_path):
#         with open(parent_directory + '/config.yaml', 'r', encoding='utf-8') as file:
#             config = yaml.safe_load(file)
#             file.close()
#         privateKey = privateKeyData().jieMi(config['privateKey'])
#         config['privateKey'] = json.loads(privateKey)
#         if config['privateKey']['privateKey']!=privateKeyData().machineCode():
#             raise ValueError("识别码非此台主机:" + privateKeyData().machineCode())
#         if nowDay() > config['privateKey']['indate']:
#             raise ValueError("系统过期请联系管理员有效期为:"+config['privateKey']['indate'])
#         return config
#
#     raise ValueError("配置文件不存在")
def openDbBase():
    if getattr(sys, 'frozen', False):
        current_file_path = sys.executable
        parent_directory = os.path.dirname(current_file_path)
    else:
        current_file_path = os.path.abspath(__file__)
        project_path = os.path.dirname(current_file_path)

        # 获取上层路径
        parent_directory = os.path.dirname(project_path)
    file_path = os.path.join(parent_directory + '/', 'config.yaml')
    if os.path.isfile(file_path):
        with open(parent_directory + '/config.yaml', 'r', encoding='utf-8') as file:
            config = yaml.safe_load(file)
            file.close()
        _,err = jieMi(config['privateKey'])
        if err!=None:
            raise ValueError(err)
        return config

    raise ValueError("配置文件不存在")
def dbConnectUrl(dbpool=None):
    if dbpool!=None:
        dbproconfig = dbpool
    else:
        dbproconfig = openDbBase()
    if dbproconfig['PASSWORD'] == None:
        dbproconfig['PASSWORD'] = ''
    PASSWORD = quote(dbproconfig['PASSWORD'])
    multipledb = {}
    # print(dbproconfig)
    if dbproconfig['DBTYPE']=='MSSQL':
        import pyodbc
        dbUrl = 'mssql+pyodbc://' + dbproconfig['USER'] + ':' + PASSWORD + '@' + dbproconfig[
            'HOST'] + ':' + str(dbproconfig['PORT']) + '/' + dbproconfig['NAME'] + '?driver=' + dbproconfig['DRIVER']

        for itm in dbproconfig['MULTIPLEDB']:
            ITMPASSWORD = quote(itm['PASSWORD'])
            multipledb[itm['NAME']] = 'mssql+pyodbc://' + itm['USER'] + ':' + ITMPASSWORD + '@' + itm[
            'HOST'] + ':' + str(itm['PORT']) + '/' + itm['NAME'] + '?driver='+ dbproconfig['DRIVER']
    else:
        dbUrl = 'mysql+pymysql://' + dbproconfig['USER'] + ':' + PASSWORD + '@' + dbproconfig['HOST'] + ':' + str(dbproconfig['PORT']) + '/' + dbproconfig['NAME']
        for itm in dbproconfig['MULTIPLEDB']:
            ITMPASSWORD = quote(itm['PASSWORD'])
            multipledb[itm['NAME']] = 'mysql+pymysql://' + itm['USER'] + ':' + ITMPASSWORD + '@' + itm['HOST'] + ':' + str(itm['PORT']) + '/' + itm['NAME']
    # print(dbUrl)
    return dbUrl,multipledb